<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <style>
        .login-container {
            width: 300px;
            margin: 100px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        .btn-login {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .error-message {
            color: red;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>用户登录</h2>
        <div class="error-message" th:if="${param.error}">
            用户名或密码错误
        </div>
        <form id="loginForm" action="/login" method="post">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <button type="submit" class="btn-login">登录</button>
        </form>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const searchParams = new URLSearchParams();
            
            for (const pair of formData) {
                searchParams.append(pair[0], pair[1]);
            }

            fetch('/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'X-Requested-With': 'XMLHttpRequest'
                },
                body: searchParams
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Login failed');
                }
                return response.json();
            })
            .then(data => {
                if (data.token) {
                    localStorage.setItem('token', data.token);
                    localStorage.setItem('userInfo', JSON.stringify(data.userInfo));
                    
                    // 使用fetch跳转到index页面
                    fetch('/index', {
                        headers: {
                            'Authorization': 'Bearer ' + data.token
                        }
                    })
                    .then(response => response.text())
                    .then(html => {
                        document.documentElement.innerHTML = html;
                        // 更新浏览器URL，但不重新加载页面
                        window.history.pushState({}, '', '/index');
                    })
                    .catch(error => {
                        console.error('Failed to load index page:', error);
                        window.location.href = '/login';
                    });
                }
            })
            .catch(error => {
                alert('登录失败，请检查用户名和密码');
            });
        });
    </script>
</body>
</html> 